const express = require('express')

const router = express.Router()
// 一级路由： /user
// 二级路由： /register
// http://127.0.0.1:3005/user/register
// 导入已封装的数据函数
const db = require('../../config/db.js')


router.post('/addMyTask', function (req, res) {
    let userName = req.body.userName
    let id = req.body.id
    let taskId = req.body.taskId
    let isFinish = req.body.isFinish
    let uid = req.body.uid
    // console.log(userName,password,phone,id);
    // console.log(req.body);
    
    let sql = 'select  * from myTask where taskId="'+taskId+'" and id="'+id+'"';
    db.query(sql, function (err, rows) {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            if (rows && rows.length !=0) {
                let [myTask] = rows
                // console.log(myTask);
                if (myTask.taskId == taskId && myTask.id == id) {
                    res.send({
                        code: 201,
                        msg: '任务已存在'
                    })
                }else {
                    res.send({
                        code:501,
                        msg:'后端异常',
                        rows
                    })
                }
                
            }else {
                let addSql = 'insert into myTask(userName,id,taskId,isFinish) values("' + userName + '","' + id + '","' + taskId + '","' + isFinish + '")'
                db.query(addSql,  (err,rows) => {
                    if (err) {
                        res.send({
                            code: -1,
                            msg: '领取失败'
                        })
                    } else {
                        res.send({
                            code: 200,
                            msg: '领取成功'
                        });
                    }
                })
            }
         
        }
    })
    
})


router.get('/myTaskList',function(req,res) {
    let id = req.query.id
    let check_sql = 'select * from myTask where id="'+id+'"';
    db.query(check_sql,(err,rows) =>{
        if(err) {
            res.send({
                code:-1,
                message:'查询失败'
            })
        }else {
            res.send({
                code:200,
                message:'查询成功',
                rows
            })
        }
    })
})


router.post('/updataIsFinish',function(req,res) {
    let taskId = req.body.taskId
    let isFinish = req.body.isFinish
    let sql = 'select * from myTask where taskId="'+taskId+'"';
    db.query(sql,(err,rows) =>{
        if(err) {
            res.send({
                code:-1,
                message:'查询失败'
            })
        }else {
            let updataSql = 'update myTask set isFinish="'+isFinish+'" where taskId="'+taskId+'"'
            console.log(isFinish,taskId);
            db.query(updataSql,  (err) => {
                if (err) {
                    res.send({
                        code: -1,
                        msg: '提交任务失败'
                    })
                } else {
                    res.send({
                        code: 200,
                        msg: '提交任务成功'
                    });
                }
            })
        }
    })
})
module.exports = router
